1
|
|
|
import { List, Map, fromJS } from 'immutable'; |
2
|
|
|
|
3
|
|
|
export const treeToFlatList = ( |
4
|
|
|
data, |
5
|
|
|
rootIdentifier = 'root', |
6
|
|
|
childIdentifier = 'children' |
7
|
|
|
) => { |
8
|
|
|
|
9
|
|
|
if (!data) { |
10
|
|
|
throw new Error('Expected data to be defined'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
const result = []; |
14
|
|
|
const cfg = { flatIndex: 0 }; |
15
|
|
|
let stack = List(); |
16
|
|
|
|
17
|
|
|
if (!Map.isMap(data)) { |
18
|
|
|
data = fromJS(data); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (data.get(rootIdentifier)) { |
22
|
|
|
data = data.get(rootIdentifier); |
23
|
|
|
|
24
|
|
|
stack = stack.push( |
25
|
|
|
toItem(List(), childIdentifier, cfg)(data) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
else { |
29
|
|
|
stack = data.get(childIdentifier).map( |
30
|
|
|
toItem(List([-1]), List([0]), childIdentifier) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
while (stack.count()) { |
35
|
|
|
|
36
|
|
|
const item = stack.first(); |
37
|
|
|
const children = item.get(childIdentifier); |
38
|
|
|
|
39
|
|
|
stack = stack.shift(); |
40
|
|
|
|
41
|
|
|
if (List.isList(children) && !item.get('_hideChildren')) { |
42
|
|
|
stack = children.map( |
43
|
|
|
toItem( |
44
|
|
|
item.get('_path').push(item.get('_id')), |
45
|
|
|
childIdentifier, |
46
|
|
|
cfg, |
47
|
|
|
item, |
48
|
|
|
children |
49
|
|
|
) |
50
|
|
|
).concat(stack); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// removing erroneous data since grid uses internal values |
54
|
|
|
result.push( |
55
|
|
|
item.delete(childIdentifier) |
56
|
|
|
.delete('parentId') |
57
|
|
|
.delete('id') |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return List(result); |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
|
const toItem = ( |
|
|
|
|
65
|
|
|
path, childIdentifier, cfg, parent, siblings = List() |
66
|
|
|
) => (node, index = 0) => { |
67
|
|
|
|
68
|
|
|
const previousSibling = index - 1 > -1 |
69
|
|
|
? siblings.get(index - 1) |
70
|
|
|
: undefined; |
71
|
|
|
|
72
|
|
|
const previousSiblingTotalChilden = ( |
73
|
|
|
previousSibling && previousSibling.get(childIdentifier) |
74
|
|
|
? previousSibling.get(childIdentifier).count() |
75
|
|
|
: 0 |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return node.merge({ |
79
|
|
|
_id: node.get('id'), |
80
|
|
|
_parentId: node.get('parentId', 'root'), |
81
|
|
|
_parentIndex: parent ? parent.get('_index') : 0, |
82
|
|
|
_depth: path.count(), |
83
|
|
|
_hideChildren: node.get('_hideChildren'), |
84
|
|
|
_hasChildren: ( |
85
|
|
|
node.get(childIdentifier) && node.get(childIdentifier).count() > 0 |
86
|
|
|
), |
87
|
|
|
_index: index, |
88
|
|
|
_flatIndex: cfg.flatIndex++, |
89
|
|
|
_isFirstChild: index === 0, |
90
|
|
|
_isLastChild: index === siblings.count() - 1, |
91
|
|
|
_previousSiblingId: previousSibling |
92
|
|
|
? previousSibling.get('id') |
93
|
|
|
: undefined, |
94
|
|
|
_previousSiblingTotalChilden: previousSiblingTotalChilden, |
95
|
|
|
_key: `tree-item-${node.get('id')}`, |
96
|
|
|
_isExpanded: ( |
97
|
|
|
node.get(childIdentifier) |
98
|
|
|
&& node.get(childIdentifier).count() > 0 |
99
|
|
|
&& !node.get('_hideChildren') |
100
|
|
|
), |
101
|
|
|
_leaf: !( |
102
|
|
|
(node.get(childIdentifier) && node.get(childIdentifier).count() > 0) |
103
|
|
|
|| (node.get('leaf') !== undefined && node.get('leaf') === false) |
104
|
|
|
), |
105
|
|
|
_path: path |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
}; |
109
|
|
|
|